<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Categories extends CI_Model {

    function __construct(){
        parent::__construct();
        $this->load->database();
    }
    
    //INSERTING DATA//
    
    function insert($input){
       $this->db->set('category_name',$input['category_name']);
       $this->db->set('category_published',$input['category_published'],FALSE);
       $this->db->insert('category');;
    }
    
    //UPDATING DATA//
    
    function update_by_id($id,$input){
        $this->db->where('category_id', $id);
        $this->db->update('category', $input);
    }
    
    //DELETING DATA//
    
    function delete_by_id($id){
        $this->db->where('category_id', $id);
        $this->db->delete('category');
    }
    
    //SELECTING DATA//
    
    function get_category($atribute,$value){
        $this->db->where($atribute,$value);
        return $this->db->get('category');
    }
    
    function get_all($join = false){
        if($join){
            $this->db->join('article', 'article.category_id = category.category_id','left');
            $this->db->select('category.category_id,category_name,category_published');
            $this->db->select('Count(article.article_id) AS total_article');
            $this->db->group_by('category.category_id'); 
        }
        return $this->db->get('category');
    }
    
    function get_category_by_id($id){
        $this->db->where('category_id',$id);
        $query = $this->db->get('category');
        return ($query->num_rows() > 0) ? $query->first_row() : null;
    }
}